import BaseAnimationPainting from "../../standard/entities/Painting/BaseAnimationPainting";
import TrackEffect from "../../standard/entities/Effect/TrackEffect";

export default class AnimationRectPainting extends BaseAnimationPainting {
    constructor(rect, color = '#f00') {
        super();
        this.rect = rect;
        this.color = color;
        this.deg = 1;
    }

    onPainterLoaded(painter) {
        super.onPainterLoaded(painter);
        painter.globalAlpha = 0.2;
        painter.fillStyle = this.color;
        this.activeDrawnStep((painter, data) => {
            painter.drawInSession(() => {
                painter.save();
                painter.beginPath();
                painter.rotateAroundPoint(data.angle, data.centre);
                painter.rectByPoint(data.firstPoint, data.width, data.height);
                painter.fill();
                painter.restore();
            });
        });
        let effect = new TrackEffect(this.drawnStep, 5, 0.05, 0.0001);
        this.setEffects([effect]);
    }

    exportDrawnData() {
        return {
            angle: this.rect.angle,
            centre: this.rect.centre,
            firstPoint: this.rect.getHornPoints()[0],
            width: this.rect.width,
            height: this.rect.height,
            color: this.color,
        }
    }


    nextFrame(painter) {
        super.nextFrame(painter);
        this.rect.rotate(1);
    }
}